import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/models/parent/linkChildDto.dart';
import 'package:ichinesereader/utils/StrUtil.dart';
import 'package:ichinesereader/utils/helper.dart';

class AddChildToParent extends StatelessWidget {
  AddChildToParent({Key? key}) : super(key: key);

  // final String userName;
  final UserController _usercontroller = Get.find();
  final controllerUsername = TextEditingController();
  final controllerSecureId = TextEditingController();

  void _onUpdate() {
    if (StrUtil.notHasValue(controllerUsername.text)) {
      showSnackMsg('Ender child\'s username!');
      return;
    }
    if (StrUtil.notHasValue(controllerSecureId.text)) {
      showSnackMsg('Ender Security Identifier!');
      return;
    }

    LinkChildDto dto = new LinkChildDto();
    dto.childUserName = controllerUsername.text;
    dto.uniqueAccessCode = controllerSecureId.text;
    _usercontroller.linkChild(dto).then((statusMessage) {
      if (statusMessage.status == 1000) {
        showSnackMsg('Link child successfully');
        Navigator.of(Get.context!).pop();
      } else {
        showSnackMsg('Failed to link child');
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text('Add Child'.tr),
      titleTextStyle: TextStyle(
          fontWeight: FontWeight.bold,
          letterSpacing: 1,
          color: Get.isDarkMode ? Color(0xffcccccc) : Colors.black,
          fontSize: 20),
      backgroundColor: Get.isDarkMode
          ? (Color(0xff333333))
          : Color.fromARGB(255, 234, 241, 238),
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(20))),
      content: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Container(
          width: 420,
          height: 210,
          child: Column(
            children: [
              Container(
                width: 400,
                child: TextField(
                  controller: controllerUsername,
                  textCapitalization: TextCapitalization.none,
                  autocorrect: false,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.black)),
                      labelText: 'Child Account Username'.tr),
                ),
              ),
              SizedBox(
                height: 8,
              ),
              Container(
                width: 400,
                child: TextField(
                  controller: controllerSecureId,
                  textCapitalization: TextCapitalization.none,
                  autocorrect: false,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.black)),
                      labelText: 'Security Identifier'.tr),
                ),
              ),
              SizedBox(
                height: 28,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  MaterialButton(
                    color: Theme.of(context).colorScheme.secondary,
                    child: Text(
                      "Cancel".tr,
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                  SizedBox(width: 20),
                  MaterialButton(
                      color: Theme.of(context).colorScheme.secondary,
                      child: Text(
                        "Submit".tr,
                        style: TextStyle(color: Colors.white),
                      ),
                      onPressed: () {
                        _onUpdate();
                      }),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}
